package com.example.gamecenter.fragments.explore

import ListRecommendAdapter
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.gamecenter.R
import androidx.recyclerview.widget.RecyclerView
import com.example.gamecenter.activitys.BaseTencentWebActivity
import com.example.gamecenter.activitys.ListActivity
import com.example.gamecenter.activitys.WebviewActivity
import com.example.gamecenter.adapter.ExploreRecyclerAdapter
import com.example.gamecenter.adapter.RecommendItem
import com.scwang.smart.refresh.footer.BallPulseFooter
import com.scwang.smart.refresh.header.BezierRadarHeader

import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import kotlinx.android.synthetic.main.activity_explore_page.*


class ExploreFragment: Fragment(R.layout.activity_explore_page) {
    // context
    private lateinit var fragmentContext: Context;
    private lateinit var adapter : ExploreRecyclerAdapter;
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState);
        fragmentContext = (context ?: this) as Context;
        val data: List<RecommendItem> = listOf(
            RecommendItem("标题1", "https://img.sxsme.com.cn/uploadimg/ico/2023/1007/1696665441196246.jpg",1, "天剑传说"),
            RecommendItem("标题2", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552459611151.jpg",2, "天剑传说"),
            RecommendItem("标题3", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552498848724.jpg",3, "天剑传说"),
            RecommendItem("标题4", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552534375775.png",4, "天剑传说"),
            RecommendItem("标题5", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552566865753.png",5, "天剑传说"),
            RecommendItem("标题6", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
            RecommendItem("标题7", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
            RecommendItem("标题8", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
        );
        setAdapter(true, false, data);
        //设置刷新逻辑
        setRefresh();
    }
    private fun initView(){
        smart_refresh_layout

    }
    /*
    * 设置刷新的方法
    * */
    private fun setRefresh() {
        // 设置头部刷新的样式
//        smart_refresh_layout.setRefreshHeader(BezierRadarHeader(fragmentContext))
        // 设置页脚刷新的样式
//        smart_refresh_layout.setRefreshFooter(BallPulseFooter(fragmentContext));
        // 设置头部刷新时间监听
        smart_refresh_layout.setOnRefreshListener {
            smart_refresh_layout.finishRefresh(2000)
            // 添加一条新数据，在最开头的位置
//            addData()
            Toast.makeText(fragmentContext, "刷新成功", Toast.LENGTH_SHORT).show()
        }
        smart_refresh_layout.setOnLoadMoreListener {
            smart_refresh_layout.finishLoadMore(2000)
            // 添加一条新数据，在最后的位置
//            addDatas()
            Toast.makeText(fragmentContext, "加载成功", Toast.LENGTH_SHORT).show()
        }
    }
    /**
     * 参数1：设置布局是否垂直 true：垂直 false：水平
     * 参数2：设置布局的方向 true:反向 false：正常
     * */
    private fun setAdapter(isVertical: Boolean, isReverse: Boolean, data: List<RecommendItem> ) {

        //创建一个适配器对象，传一个集合进去给适配器
        adapter = ExploreRecyclerAdapter(data);
        //设置垂直还是水平，三元表达式
        val manager: LinearLayoutManager = LinearLayoutManager(fragmentContext);
        manager.orientation = if (isVertical) RecyclerView.VERTICAL else RecyclerView.HORIZONTAL

        // 设置方向
        manager.reverseLayout = false

        // 把设置好的方向和排版给 RecyclerView
        recycler_view.layoutManager = manager

        adapter.setOnDetailClickListener(object : ExploreRecyclerAdapter.OnDetailItemClickListener{
            override fun onDetail(v: View?, parmas: RecommendItem?) {
                // 处理点击事件
                println("点击事件")
                val intent = Intent(fragmentContext, BaseTencentWebActivity().javaClass)
                startActivity(intent)
//                if (v?.id == R.id.recommend_item_view) { // 查看更多
//                    println("点击事件")
//                    val intent = Intent(fragmentContext, WebviewActivity().javaClass)
//                    startActivity(intent)
//                }
            }
        })
        // 设置适配器
        recycler_view.adapter = adapter
    }
}